#include <stdio.h>
#include <string.h>
#include "List.h"

List::List()
{
	this->fd = -1;
    this->next = NULL;
}



List::List(int fd, const char *fileName)
{
	this->fd = fd;
	strcpy(this->fileName, fileName);
    this->next = NULL;
}

List::~List()
{
	
}


void List::addItem(List *item)
{
	List *p = this;
    while( p->next != NULL )
    {
        p = p->next;
    }

    p->next = item;
}


void List::delItem(int fd)
{
	List *p = this;
    while( p->next != NULL )
    {
    	if( p->next->fd == fd )
    	{
    		List *q = p->next;
    		p->next = p->next->next;	
    		delete q;
    		continue;
    	}

        p = p->next;
    }
}



void List::delItem(const char *fileName)
{
	List *p = this;
    while( p->next != NULL )
    {
    	if( strcmp(p->next->fileName,fileName) == 0 )
    	{
    		List *q = p->next;
    		p->next = p->next->next;	
    		delete q;
    		continue;
    	}
        p = p->next;
    }
}


List *List::getItemByFileName(const char *fileName)
{
	List *p = this;
    while( p->next != NULL )
    {
    	if( strcmp(p->next->fileName,fileName) == 0 )
    	{
    		return p->next;
    	}
        p = p->next;
    }
}


List *List::getItemByFd(int fd)
{
	List *p = this;
    while( p->next != NULL )
    {
    	if( p->next->fd == fd )
    	{
    		return p->next;
    	}

        p = p->next;
    }
    return NULL;
}


void List::showList()
{
	List *p = this;
    while( p->next != NULL )
    {
    	printf("%d  %s\n",p->next->fd, p->next->fileName);
        p = p->next;
    }
}